package sample.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import sample.web.model.Permisson;
import sample.web.model.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by zhaopeng on 2/29/16.
 */
@Service
public class UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<User> findAll(){
        return jdbcTemplate.query("SELECT * FROM user", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setEmail(rs.getString("email"));
                return user;
            }
        });
    }

    //验证该登录用户是否存在
    public int getUserByLoginInfo(User user){
        String sql="SELECT * FROM user WHERE username='"+user.getUsername()+"' and password='"+user.getPassword()+"'";
        //List userList=jdbcTemplate.queryForList(sql,List.class);
        List<User> userList=jdbcTemplate.query(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setEmail(rs.getString("email"));
                user.setPassword(rs.getString("password"));
                user.setMobile(rs.getString("mobile"));
                user.setRoleId(rs.getInt("roleId"));
                return user;
            }
        });
        if (userList.size()!=0){
            return userList.get(0).getRoleId();
        }else{
            return 0;
        }


    }

    //通过 roleid 获取资源信息
    public List<Permisson> findByRoleId(int roleid){
        String sql="";
        if(roleid!=0){
            if (roleid==1){
                sql="select * from permission";
            }else {
                sql="select * from permission p left join rolePermission rp on p.id=rp.permissionId where rp.roleId="+roleid;
            }}
            List<Permisson> permissonsList=jdbcTemplate.query(sql, new RowMapper<Permisson>() {
            @Override
            public Permisson mapRow(ResultSet rs, int rowNum) throws SQLException {
                Permisson permisson = new Permisson();
                permisson.setId(rs.getInt("id"));
                permisson.setName(rs.getString("name"));
                permisson.setMethod(rs.getString("method"));
                permisson.setValue(rs.getString("value"));
                permisson.setUrl(rs.getString("url"));
                return permisson;
            }
        });
            return permissonsList;
    }

    //保存注册信息
    public void saveByReginInfo(User user){
        String sql="INSERT INTO user (username,email,mobile,password,roleId) VALUES('"+user.getUsername()
                +"','"+user.getEmail()+"','"+user.getMobile()+"','"+user.getPassword()+"',"+user.getRoleId()+")";
        jdbcTemplate.execute(sql);
    }
}